package com.candzen.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;


public class GoogleAuthentication {
	private final String gaeAppBaseUrl = "http://idow4u.appspot.com/";
	private final String gaeAppLoginUrl = gaeAppBaseUrl + "_ah/login";
	private final String googleLoginUrl = "https://www.google.com/accounts/ClientLogin";
	private final String service = "ah";
	private Cookie authCookie = null;

	// sample function that makes an http request and downloads
	// the result providing the auth cookie.
	public String postIntent(String what, String when, String where) throws IOException {
		//TODO: Check authCookie here as a precondition
		try {
			StringBuilder uri = new StringBuilder(gaeAppBaseUrl);
			uri.append( "?what=" + URLEncoder.encode(what + "&when=" + when + "&where=" + where, "UTF-8") );
			
			HttpPost request = new HttpPost(uri.toString());
			request.setHeader("Cookie", authCookie.getName() + "=" + authCookie.getValue());
			DefaultHttpClient client = new DefaultHttpClient();
			HttpResponse response = client.execute(request);
			Log.d(getClass().getName(), response.getStatusLine().toString());
			return response.toString();
		}
		catch(IOException e) {
			Log.w(this.getClass().getName(), e);
			e.printStackTrace();
			return "Request failed.";
		}
	}

	// Constructor creates the cookie
	public GoogleAuthentication(String googleAccount, String googlePassword) {
		if (authCookie == null) {

			try {
				String authToken = getToken(googleAccount, googlePassword);
				authCookie = getAuthCookie(authToken);
				Log.d(this.getClass().getName(), "AuthToken: " + authToken);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d(this.getClass().getName(), e.getMessage());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d(this.getClass().getName(), e.getMessage());
			}

		}
	}

	private String getToken(String googleAccount, String googlePassword)
			throws MalformedURLException, ProtocolException,
			UnsupportedEncodingException, IOException {
		String token = null;
		HttpURLConnection h = getConnection(googleAccount, googlePassword);
		token = extractAuthTokenFromResponse(h);
		return token;
	}

	private Cookie getAuthCookie(String authToken)
			throws ClientProtocolException, IOException {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		Cookie retObj = null;
		String cookieUrl = gaeAppLoginUrl + "?continue="
				+ URLEncoder.encode(gaeAppBaseUrl, "UTF-8") + "&auth="
				+ URLEncoder.encode(authToken, "UTF-8");
		HttpGet httpget = new HttpGet(cookieUrl);
		HttpResponse response = httpClient.execute(httpget);
		if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK
				|| response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_NO_CONTENT) {

			if (httpClient.getCookieStore().getCookies().size() > 0) {
				retObj = httpClient.getCookieStore().getCookies().get(0);
			}

		}
		return retObj;

	}

	private HttpURLConnection getConnection(String username, String password)
			throws MalformedURLException, IOException, ProtocolException,
			UnsupportedEncodingException {
		HttpURLConnection urlConnection;
		URL url = new URL(googleLoginUrl);
		urlConnection = (HttpURLConnection) url.openConnection();
		urlConnection.setRequestMethod("POST");
		urlConnection.setDoInput(true);
		urlConnection.setDoOutput(true);
		urlConnection.setUseCaches(false);
		urlConnection.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");
		StringBuilder content = new StringBuilder();
		content.append("Email=").append(username);
		content.append("&Passwd=").append(password);
		content.append("&service=").append(service);

		OutputStream outputStream = urlConnection.getOutputStream();
		outputStream.write(content.toString().getBytes("UTF-8"));
		outputStream.close();
		return urlConnection;
	}

	private String extractAuthTokenFromResponse(HttpURLConnection urlConnection)
			throws IOException {
		int responseCode = urlConnection.getResponseCode();
		System.out.println(responseCode);
		StringBuffer resp = new StringBuffer();
		if (responseCode == HttpURLConnection.HTTP_OK) {
			InputStream inputStream = urlConnection.getInputStream();

			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inputStream));
			String line;

			while ((line = rd.readLine()) != null) {

				if (line.startsWith("Auth=")) {
					resp.append(line.substring(5));

				}
			}

			rd.close();

		}
		return resp.toString();
	}
}
